//
//  ComplaintDetailViewController.m
//  shuiyun
//
//  Created by 徐双 on 2017/9/7.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "ComplaintDetailViewController.h"
#import "ContentViewCell.h"
#import "BoatDetailViewCell.h"
#import "UserCommentViewCell.h"



@interface ComplaintDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSString * saytext;
}

@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)NSMutableDictionary * dict;

@property(nonatomic,strong)UILabel * label2;

@property(nonatomic,strong)NSMutableArray * arr;

@end

@implementation ComplaintDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self ComplaintDetail];
    [self setTitle:@"投诉详情"];
    [self setUpFirstView];
    [self setUpTableview];
    [self setUpBottom];
}

-(void)setUpFirstView{
    
    UILabel * label1 = [[UILabel alloc] init];
    [self.view addSubview:label1];
    label1.text = @"发布信息:";
    label1.font = [UIFont systemFontOfSize:17];
    label1.textColor = [UIColor colorWithRed:129/255.0f green:129/255.0f blue:129/255.0f alpha:1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(80);
        make.top.equalTo(self.view).with.offset(5);
        make.width.offset(80);
        make.height.offset(25);
        
    }];
    
    UILabel * label2 = [[UILabel alloc] init];
    [self.view addSubview:label2];
    self.label2 = label2;
    label2.font = [UIFont systemFontOfSize:17];
    label2.textColor = [UIColor colorWithRed:129/255.0f green:129/255.0f blue:129/255.0f alpha:1];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(label1.mas_right).with.offset(5);
        make.centerY.equalTo(label1.mas_centerY);
        make.width.offset(400);
        make.height.offset(25);
        
    }];
    
}

-(void)setUpTableview{
    
    
    //初始化tableView
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 35, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 35 - 40);
    
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.estimatedRowHeight = 800;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //   self.tableView.estimatedRowHeight = 400;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ContentViewCell" bundle:nil] forCellReuseIdentifier:@"ContentViewCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BoatDetailViewCell" bundle:nil] forCellReuseIdentifier:@"BoatDetailViewCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"UserCommentViewCell" bundle:nil] forCellReuseIdentifier:@"UserCommentViewCell"];
    
    
}


-(void)ComplaintDetail{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;

    _dict = [NSMutableDictionary dictionary];
    
    _arr = [NSMutableArray array];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:token forKey:@"token"];
    NSString * userid = [PublicFunction getConfigWithKey:@"userid"];
    [productParams setValue:userid forKey:@"userid"];
    //NSString * id = [PublicFunction getConfigWithKey:@"id"];
    [productParams setValue:self.idStr forKey:@"id"];
    
    
    [[MNetworkHandler sharedInstance]conURL:boatComplaintDetail networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        NSLog(@"%@", returnData);
        NSLog(@"%@", productParams);
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            _dict = [returnData objectForKey:@"data"];
            NSLog(@"!!!!!!!!!!!!!!!%@", _dict);
            _arr = [_dict objectForKey:@"Comment"];
            
            self.label2.text = [_dict valueForKey:@"s_time"];
            [self.tableView reloadData];
            
            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [hud removeFromSuperview];
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        [hud removeFromSuperview];        
    }];
    
 
}
#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        
        return 54;
    }
    
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section == 2) {
        
        UIView * headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 44)];
        
        //headView.backgroundColor = [UIColor redColor];
        
        UIView * grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 10)];
        
        grayView.backgroundColor = getColor(@"F5F5F5");
        
        [headView addSubview:grayView];
        
        UIView * blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 15, 4, 16)];
        
        blueView.backgroundColor = getColor(@"3F87F6");
        
        [headView addSubview:blueView];
        
        UILabel * label = [[UILabel alloc] init];
        [headView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(blueView).with.offset(10);
            make.centerY.equalTo(blueView.mas_centerY);
            make.height.offset(20);
            make.width.offset(60);
            
        }];
        
        label.text = @"用户评论";
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = getColor(@"262626");
        
        UIView * sepView = [[UIView alloc]initWithFrame:CGRectMake(0, 52,DEF_SCREEN_WIDTH,2)];
        [headView addSubview:sepView];
      
        
        sepView.backgroundColor = getColor(@"F5F5F5");
        return headView;
    }
    
    return nil;
}


#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    if (section == 0) {
        
        return 5;
    }else if(section == 1){
        
        return 3;
        
    }else{
    
        return _arr.count;
    }
   
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 4) {
            
             return UITableViewAutomaticDimension;
        }else{
        
            return 50;
        }
        
    }else if(indexPath.section == 1){
    
        return 50;
    
    }else {
    
        
        return UITableViewAutomaticDimension;
    
    }
    
    
}

#pragma mark - ---------- 每个Cell的内容 ----------
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.section == 0) {
        
        BoatDetailViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BoatDetailViewCell"];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        if (indexPath.row == 0) {
            
            cell.nameLabel.text = @"投诉类别";
            cell.contentLabel.text = [_dict objectForKey:@"type"];
        }else if (indexPath.row == 1){
            
            cell.nameLabel.text = @"被投诉公司";
            cell.contentLabel.text = [_dict objectForKey:@"d_company"];

            
        }else if (indexPath.row == 2){
            
            cell.nameLabel.text = @"被投诉人";
            cell.contentLabel.text = [_dict objectForKey:@"defendant"];
            
        }else if (indexPath.row == 3){
            
            cell.nameLabel.text = @"被投诉人手机";
            cell.contentLabel.text = [_dict objectForKey:@"d_phone"];
            
        }else{
            ContentViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ContentViewCell"];
            cell.selectionStyle = UITableViewCellEditingStyleNone;
            
            cell.beizhuLabel.text = @"投诉内容";
            
            cell.contentLabel.text = [_dict objectForKey:@"content"];
            [cell.contentLabel sizeToFit];
            
            return cell;
        }
        return cell;
        
    
    }else if(indexPath.section == 1){
        
        BoatDetailViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BoatDetailViewCell"];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        
        if (indexPath.row == 0) {
            
            cell.nameLabel.text = @"投诉人";
            cell.contentLabel.text = [_dict objectForKey:@"plaintiff"];
        }else if (indexPath.row == 1){
            
            cell.nameLabel.text = @"投诉人手机";
            cell.contentLabel.text = [_dict objectForKey:@"p_phone"];
        }else {
            
            cell.nameLabel.text = @"投诉人公司";
            cell.contentLabel.text = [_dict objectForKey:@"p_company"];
        }
        
        return cell;
    }else{
    
        UserCommentViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UserCommentViewCell"];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        
         NSDictionary * dict1 = _arr[indexPath.row];
        cell.contentLabel.text = [NSString stringWithFormat:@"%@:%@",[dict1 objectForKey:@"username"],[dict1 objectForKey:@"saytext"]];
   
        return cell;
    }
}

-(void)setUpBottom{

    //--------------背景颜色
    UIView *bottomView = [[UIView alloc]init];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(0);
        make.height.offset(40);
        
    }];
    bottomView.backgroundColor = getColor(@"F8F6F6");
    
    //---------------文本框
    UITextField *field = [[UITextField alloc] init];
    [bottomView addSubview:field];
    field.placeholder = @" 说点什么吧......";
    field.textColor = getColor(@"C9C9C9");
    field.layer.cornerRadius = 5.0;
    field.text = saytext;
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomView).offset(16);
        make.bottom.equalTo(bottomView).offset(-5);
        make.height.offset(30);
        make.width.offset(300);
        
    }];
    field.backgroundColor = getColor(@"F8F6F6");
    
    
    //--------------发送按钮
    UIButton *buttonOut = [UIButton buttonWithType:UIButtonTypeCustom];
    [bottomView addSubview:buttonOut];
    [buttonOut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bottomView).offset(-16);
        make.bottom.equalTo(bottomView).offset(-5);
        make.height.offset(30);
        make.width.offset(50);
    }];
    [buttonOut setTitle:@"发送" forState:UIControlStateNormal];
    buttonOut.backgroundColor = getColor(@"3f87f6");
    buttonOut.layer.cornerRadius = 5.0;
    [buttonOut setTitleColor:getColor(@"FFFFFF") forState:UIControlStateNormal];
    buttonOut.titleLabel.font = [UIFont systemFontOfSize:15];
    [buttonOut addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}

-(void)btnClick{

    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:token forKey:@"token"];
    NSString * userid = [PublicFunction getConfigWithKey:@"userid"];
    [productParams setValue:userid forKey:@"userid"];
    //NSString * id = [PublicFunction getConfigWithKey:@"id"];
    [productParams setValue:self.idStr forKey:@"id"];
    [productParams setValue:saytext forKey:@"saytext"];
    
    
    [[MNetworkHandler sharedInstance]conURL:boatComplaintDetail networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        NSLog(@"%@", returnData);
        NSLog(@"%@", productParams);
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            [self.tableView reloadData];
            
            [self.navigationController popViewControllerAnimated:YES];
   
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
        
    }];
    
    

}
@end
